/*
 * @Author: lvyinling lvyinling@smartlink.com.cn
 * @Date: 2022-07-04 17:05:01
 * @LastEditors: lvyinling lvyinling@smartlink.com.cn
 * @LastEditTime: 2022-08-03 10:52:02
 * @FilePath: /react-vite-template/src/pages/p-cnpc/services/voucher/index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import axios from "../request";
import config from "./config";
import { DTO, Params, Types } from "./type.d";

/* 获取面值 */
export function getFaceValueList(params: { channelId: string }) {
  return axios.post<CNPC.Res.page<DTO.BasicList>>(
    config.getFaceValueList,
    params
  );
}

/* 加油券列表 */
export function getBaseCouponsList() {
  // params: Omit<Params.ListSearchParams, keyof CNPC.Req.Pagination>
  return axios.post<CNPC.Res.data<Types.BaseCoupons[]>>(
    config.getBaseCouponsList,
    // params
    {}
  );
}
/* 获取名下待使用加油卡券列表 */
export async function queryUnusedList(params: Params.ListSearchParams) {
  try {
    const res = await axios.post<CNPC.Res.page<DTO.SearchVoucher>>(
      config.queryUnusedList,
      params
    );
    return {
      list: res.data.data.records,
      total: res.data.data.total,
    };
  } catch (e) {
    return {
      list: [],
      total: 0,
    };
  }
}
/* 获取名下已失效加油卡券列表 */
export async function queryInvalidList(params: Params.ListSearchParams) {
  try {
    const res = await axios.post<CNPC.Res.page<DTO.SearchVoucher>>(
      config.queryInvalidList,
      params
    );
    return {
      list: res.data.data.records,
      total: res.data.data.total,
    };
  } catch (error) {
    return {
      list: [],
      total: 0,
    };
  }
}
/* 获取名下已使用加油卡券列表 */
export async function queryUsedList(params: Params.ListSearchParams) {
  try {
    const res = await axios.post<CNPC.Res.page<DTO.SearchVoucher>>(
      config.queryUsedList,
      params
    );
    return {
      list: res.data.data.records,
      total: res.data.data.total,
    };
  } catch (error) {
    return {
      list: [],
      total: 0,
    };
  }
}
/* 加油券详情-待使用 */
export async function couponsUnusedDetail(params: Params.CouponsId) {
  const res = await axios.post<CNPC.Res.data<DTO.VoucherDetail>>(
    config.couponsUnusedDetail,
    params,
    { loading: true }
  );
  return res.data.data;
}
/* 加油券详情-已过期 */
export async function couponsExpireDetail(params: Params.CouponsId) {
  const res = await axios.post<CNPC.Res.data<DTO.VoucherDetail>>(
    config.couponsExpireDetail,
    params,
    { loading: true }
  );
  return res.data.data;
}
/* 加油券核销码刷新 */
export async function couponsCheckcode(params: Params.CouponsId) {
  const res = await axios.post<CNPC.Res.data<{ checkCode: string }>>(
    config.couponsCheckcode,
    params
  );
  return res.data;
}
/* 发起退款 */
export async function refund(params: Params.CouponsId) {
  const res = await axios.post<CNPC.Res.data<null>>(config.refund, params, {
    loading: true,
  });
  return res.data;
}
/* 失效券兑换 */
export async function exchange(params: Params.CouponsId) {
  const res = await axios.post<CNPC.Res.data<null>>(config.exchange, params);
  return res.data;
}
/* 加油券可用加油站的数量 */
export async function usableStationNumber(params: { couponsId: string }) {
  const res = await axios.post<CNPC.Res.data<null>>(
    config.usableStationNumber,
    params
  );
  return res.data;
}
/* 查询核销状态 */
export async function isChecked(params: Params.CouponsId) {
  const res = await axios.post<CNPC.Res.data<{ checked: boolean }>>(
    config.isChecked,
    params
  );
  return res.data;
}
/* 获取加油券基本信息 */
export async function getCouponsBaseInfo(params: { couponsId: string }) {
  try {
    const res = await axios.post<CNPC.Res.data<{ instruction: string }>>(
      config.getCouponsBaseInfo,
      params
    );
    return res.data.data.instruction;
  } catch (error) {
    return "";
  }
}

export * from "./type.d";
